package com.ruoyi.mp.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.MpClientReturnRecord;
import com.ruoyi.db.domain.system.PcClientReturnRecord;
import com.ruoyi.db.domain.system.PcReturnConfig;
import com.ruoyi.db.domain.web.MpIncomeRecord;
import com.ruoyi.db.mapper.system.MpClientMapper;
import com.ruoyi.db.mapper.system.MpClientReturnRecordMapper;
import com.ruoyi.db.mapper.system.PcClientReturnRecordMapper;
import com.ruoyi.db.mapper.system.PcReturnConfigMapper;
import com.ruoyi.db.mapper.web.MpIncomeRecordMapper;
import com.ruoyi.mp.service.IMpClientReturnRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 用户参与定返活动记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-05
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpClientReturnRecordServiceImpl implements IMpClientReturnRecordService
{
    @Autowired
    private MpClientReturnRecordMapper mpClientReturnRecordMapper;
    @Autowired
    private PcReturnConfigMapper pcReturnConfigMapper;
    @Autowired
    private MpIncomeRecordMapper mpIncomeRecordMapper;
    @Autowired
    private MpClientMapper mpClientMapper;
    @Autowired
    private PcClientReturnRecordMapper pcClientReturnRecordMapper;

    /**
     * 查询用户参与定返活动记录
     * 
     * @param id 用户参与定返活动记录主键
     * @return 用户参与定返活动记录
     */
    @Override
    public MpClientReturnRecord selectMpClientReturnRecordById(Long id)
    {
        return mpClientReturnRecordMapper.selectMpClientReturnRecordById(id);
    }

    /**
     * 查询用户参与定返活动记录列表
     * 
     * @param mpClientReturnRecord 用户参与定返活动记录
     * @return 用户参与定返活动记录
     */
    @Override
    public List<MpClientReturnRecord> selectMpClientReturnRecordList(MpClientReturnRecord mpClientReturnRecord)
    {
        List<MpClientReturnRecord> list = mpClientReturnRecordMapper.selectMpClientReturnRecordList(mpClientReturnRecord);
        for (MpClientReturnRecord returnRecord:list) {
            PcReturnConfig pcReturnConfig = pcReturnConfigMapper.selectPcReturnConfigById(returnRecord.getReturnId());
            returnRecord.setReturnName(pcReturnConfig.getReturnName());
        }
        return list;
    }

    /**
     * 新增用户参与定返活动记录
     * 
     * @param mpClientReturnRecord 用户参与定返活动记录
     * @return 结果
     */
    @Override
    public void insertMpClientReturnRecord(MpClientReturnRecord mpClientReturnRecord,MpClient client)
    {
        Date now = new Date();
        PcReturnConfig pcReturnConfig = pcReturnConfigMapper.selectPcReturnConfigById(mpClientReturnRecord.getReturnId());
        if(now.after(pcReturnConfig.getStartTime())&&now.before(pcReturnConfig.getEndTime())){
            if(mpClientReturnRecord.getBuyPartCount()==0||mpClientReturnRecord.getBuyPartCount()<pcReturnConfig.getMixPart()){
                throw new ServiceException("不能少于最少购买份数");
            }
            if(mpClientReturnRecord.getBuyPartCount()+pcReturnConfig.getSelledPart()>pcReturnConfig.getTotalPart()){
                throw new ServiceException("活动总份数不足");
            }
            BigDecimal totalAmount = pcReturnConfig.getPartAmount()
                    .multiply(new BigDecimal(mpClientReturnRecord.getBuyPartCount().toString())).setScale( 2, BigDecimal.ROUND_HALF_UP );
            if(client.getSilverBeanBalance().compareTo(totalAmount)<0){
                throw new ServiceException("用户银藻余额不足");
            }
            MpClientReturnRecord returnRecord = new MpClientReturnRecord();
            returnRecord.setClientId(mpClientReturnRecord.getClientId());
            returnRecord.setReturnId(mpClientReturnRecord.getReturnId());
            List<MpClientReturnRecord> returnRecords = mpClientReturnRecordMapper.selectMpClientReturnRecordList(returnRecord);
            if(returnRecords.size()>0){
                MpClientReturnRecord old = returnRecords.get(0);
                old.setBuyPartCount(old.getBuyPartCount()+mpClientReturnRecord.getBuyPartCount());
                old.setTakeTime(now);
                old.setTakeAmount(old.getTakeAmount().add(totalAmount));
                mpClientReturnRecordMapper.updateMpClientReturnRecord(old);
            }else {
                mpClientReturnRecord.setClientId(client.getClientId());
                mpClientReturnRecord.setTakeTime(now);
                mpClientReturnRecord.setIsSettle(0);
                mpClientReturnRecord.setTakeAmount(totalAmount);
                mpClientReturnRecord.setHaveReturnAmount(new BigDecimal(0));
                mpClientReturnRecordMapper.insertMpClientReturnRecord(mpClientReturnRecord);
            }
            client.setSilverBeanBalance(client.getSilverBeanBalance().subtract(totalAmount));
            mpClientMapper.updateMpClient(client);
            MpIncomeRecord mpIncomeRecord = new MpIncomeRecord();
            mpIncomeRecord.setAmount(totalAmount);
            mpIncomeRecord.setIsIncome(0l);
            mpIncomeRecord.setType(2l);
            mpIncomeRecord.setClientId(client.getClientId());
            mpIncomeRecord.setCurrentBalance(client.getSilverBeanBalance());
            mpIncomeRecord.setCreateTime(now);
            mpIncomeRecord.setContent("参与定返活动扣除银藻");
            mpIncomeRecordMapper.insertMpIncomeRecord(mpIncomeRecord);
        }else{
            throw new ServiceException("不在活动时间内");
        }
        pcReturnConfig.setSelledPart(pcReturnConfig.getSelledPart()+mpClientReturnRecord.getBuyPartCount());
        pcReturnConfigMapper.updatePcReturnConfig(pcReturnConfig);
    }

    /**
     * 修改用户参与定返活动记录
     * 
     * @param mpClientReturnRecord 用户参与定返活动记录
     * @return 结果
     */
    @Override
    public int updateMpClientReturnRecord(MpClientReturnRecord mpClientReturnRecord)
    {
        return mpClientReturnRecordMapper.updateMpClientReturnRecord(mpClientReturnRecord);
    }

    /**
     * 批量删除用户参与定返活动记录
     * 
     * @param ids 需要删除的用户参与定返活动记录主键
     * @return 结果
     */
    @Override
    public int deleteMpClientReturnRecordByIds(Long[] ids)
    {
        return mpClientReturnRecordMapper.deleteMpClientReturnRecordByIds(ids);
    }

    /**
     * 删除用户参与定返活动记录信息
     * 
     * @param id 用户参与定返活动记录主键
     * @return 结果
     */
    @Override
    public int deleteMpClientReturnRecordById(Long id)
    {
        return mpClientReturnRecordMapper.deleteMpClientReturnRecordById(id);
    }

    @Override
    public List<PcClientReturnRecord> getPcClientRaiseRecordList(Long id) {
        PcClientReturnRecord returnRecord = new PcClientReturnRecord();
        returnRecord.setClientReturnRecordId(id);
        List<PcClientReturnRecord> list = pcClientReturnRecordMapper.selectPcClientReturnRecordList(returnRecord);
        return list;
    }
}
